package com.vim.modules.web.api;

import com.vim.common.ShopConstants;
import com.vim.common.annotation.ParentModuleInfo;
import com.vim.common.model.response.RestfulResponse;
import com.vim.common.utils.ValidateUtils;
import com.vim.modules.web.model.ShopUserAddress;
import com.vim.modules.web.service.ShopUserAddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@ParentModuleInfo(moduleCode = "shop", moduleName = "电商平台")
@Api(value = "用户地址管理", description = "获取用户地址列表和用户地址详情")
@RestController
@RequestMapping(value = "/api/shop/address/")
public class ApiShopAddressController {

    @Resource
    private ShopUserAddressService shopUserAddressService;

    @ApiOperation(value = "获取用户地址列表", notes = "{\"code\":20000,\"msg\":\"OK\",\"data\":[{\"addressId\":\"e3f888dcbb0447129901ed3105447996\",\"buyerId\":\"2796602e821144678e2084ce2f82a672\",\"buyerName\":\"小何\",\"buyerPhone\":\"15528047640\",\"deliveryProvinceId\":\"42\",\"deliveryProvince\":\"湖北省\",\"deliveryCityId\":\"4201\",\"deliveryCity\":\"武汉市\",\"deliveryAreaId\":\"420115\",\"deliveryArea\":\"江夏区\",\"deliveryStreet\":\"金融港\",\"isDefault\":\"1\",\"addressInfo\":null}]}", httpMethod="GET")
    @ApiImplicitParams({@ApiImplicitParam(name = "userId", value = "用户ID", required = true)})
    @RequestMapping(value = "/userAddressList")
    public RestfulResponse userAddressList(@RequestParam(value = "userId") String userId){
        ValidateUtils.checkNotBlank(userId);
        ShopUserAddress address = new ShopUserAddress();
        address.setBuyerId(userId);
        return new RestfulResponse(shopUserAddressService.findList(address));
    }

    @ApiOperation(value = "获取用户默认地址", notes = "{\"code\":20000,\"msg\":\"OK\",\"data\":{\"addressId\":\"e3f888dcbb0447129901ed3105447996\",\"buyerId\":\"2796602e821144678e2084ce2f82a672\",\"buyerName\":\"小何\",\"buyerPhone\":\"15528047640\",\"deliveryProvinceId\":\"42\",\"deliveryProvince\":\"湖北省\",\"deliveryCityId\":\"4201\",\"deliveryCity\":\"武汉市\",\"deliveryAreaId\":\"420115\",\"deliveryArea\":\"江夏区\",\"deliveryStreet\":\"金融港\",\"isDefault\":\"1\",\"addressInfo\":null}}", httpMethod="GET")
    @ApiImplicitParams({@ApiImplicitParam(name = "userId", value = "用户ID", required = true)})
    @RequestMapping(value = "/userDefaultAddress")
    public RestfulResponse userDefaultAddress(@RequestParam(value = "userId") String userId){
        ValidateUtils.checkNotBlank(userId);
        ShopUserAddress address = new ShopUserAddress();
        address.setBuyerId(userId);
        address.setIsDefault(ShopConstants.USER_ADDRESS.DEFAULT_ADDRESS);
        List<ShopUserAddress> list = shopUserAddressService.findList(address);
        return new RestfulResponse(list.size() == 0 ? new ArrayList<>() : list.get(0));
    }

    @ApiOperation(value = "获取用户地址详情", notes = "{\"code\":20000,\"msg\":\"OK\",\"data\":{\"addressId\":\"1\",\"buyerId\":\"1\",\"buyerName\":\"小刘\",\"buyerPhone\":\"13999999999\",\"deliveryProvinceId\":\"\",\"deliveryProvince\":\"42\",\"deliveryCityId\":\"\",\"deliveryCity\":\"4201\",\"deliveryAreaId\":\"\",\"deliveryArea\":\"420101\",\"deliveryStreet\":\"金融港\",\"isDefault\":\"0\"}}", httpMethod="GET")
    @ApiImplicitParams({@ApiImplicitParam(name = "addressId", value = "地址ID", required = true)})
    @RequestMapping(value = "/userAddressDetail")
    public RestfulResponse userAddressDetail(@RequestParam(value = "addressId") String addressId){
        ValidateUtils.checkNotBlank(addressId);
        return new RestfulResponse(shopUserAddressService.findAddress(addressId));
    }

    @ApiOperation(value = "修改用户地址", notes = "{\"code\":20000,\"msg\":\"OK\",\"data\":\"OK\"}", httpMethod="POST")
    @RequestMapping(value = "/editUserAddress", method = RequestMethod.POST)
    public RestfulResponse editUserAddress(ShopUserAddress address){
        ValidateUtils.checkNotBlank(address.getAddressId());
        ValidateUtils.checkNotBlank(address.getBuyerId());
        ValidateUtils.checkNotBlank(address.getBuyerName());
        ValidateUtils.checkNotBlank(address.getBuyerPhone());
        ValidateUtils.checkNotBlank(address.getDeliveryProvinceId());
        ValidateUtils.checkNotBlank(address.getDeliveryCityId());
        ValidateUtils.checkNotBlank(address.getDeliveryAreaId());

        ShopUserAddress findDefault = new ShopUserAddress();
        findDefault.setBuyerId(address.getBuyerId());
        findDefault.setIsDefault(ShopConstants.USER_ADDRESS.DEFAULT_ADDRESS);
        List<ShopUserAddress> list = shopUserAddressService.findList(findDefault);
        if(null == list || list.size() == 0){
            address.setIsDefault(ShopConstants.USER_ADDRESS.DEFAULT_ADDRESS);
        }
        shopUserAddressService.editAddress(address);
        return new RestfulResponse(RestfulResponse.OK);
    }
}
